package my.springboot.validation.verify;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.junit.Test;

public class CharCheckTest {

	@org.junit.jupiter.api.Test
	public void test(){
		System.out.println("11111");
		try {
			System.out.println("1");
			CharCheckTest.validate(new User("张三"));
			System.out.println("2");
			CharCheckTest.validate(new User("张三kkk"));
			System.out.println("3");
			CharCheckTest.validate(new User("张三123"));
			System.out.println("4");
			CharCheckTest.validate(new User("张三,，😄"));
			System.out.println("结束");
		} catch (ValidationException e) {
			System.out.println("【错误】"+e.getMessage());
		}
	}
	
	@Test
	public void test1(){
		try {
			User t = new User("张三");
			List<String> otherNames = new ArrayList<String>();
			otherNames.add("张三😄");
			t.setOtherNames(otherNames);
			CharCheckTest.validate(t);
			System.out.println("结束");
		} catch (ValidationException e) {
			System.out.println("【错误】"+e.getMessage());
		}
	}
	
	public static <T>  void validate(T t) throws ValidationException {  
        ValidatorFactory vFactory = Validation.buildDefaultValidatorFactory();  
        Validator validator = vFactory.getValidator();  
        Set<ConstraintViolation<T>> set =  validator.validate(t);  
        if(set.size()>0){  
            for(ConstraintViolation<T> val : set){  
                throw new ValidationException(val.getMessage());              
            }  
        }  
    }  
}
